package cn.ck.inteceptor;

import cn.ck.util.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInteceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute(Constants.USER_SESSION);
        if(loginUser!=null){
            return true;//将请求放行给控制器
        }
        //重订向回登陆
        //动态工程名
        // ${pageContext.request.contextPath}

        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/toLogin");
        return false;
    }
}
